<template>
  <div class="container">
    <div class="left">
      <h3>叩丁狼电商运营平台</h3>
      <el-icon color="#fff" size="20" @click="isCollapse = !isCollapse">
        <Menu />
      </el-icon>
    </div>
    <div class="right">
      <el-dropdown @command="handleCommand">
        <span class="el-dropdown-link">
          {{ store.username }}
          <el-icon class="el-icon--right">
            <arrow-down />
          </el-icon>
        </span>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item command="index">
              首页
            </el-dropdown-item>
            <el-dropdown-item command="logout">退出</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
  </div>
</template>

<script setup lang="ts">
import { Menu, ArrowDown } from '@element-plus/icons-vue'
import { useRouter } from 'vue-router'
import { inject } from 'vue'
import { useUserStore } from '@/store/user'
import { ElMessage } from 'element-plus'
import { logout } from '@/common/index'
import { ApiLogout } from '@/components/header/api'

const store = useUserStore()

const isCollapse = inject<boolean>('isCollapse')

const router = useRouter()

// 清除token
const handleCommand = (command: string) => {
  if (command === 'index') {
    router.push('/index')
  } else if (command === 'logout') {
    logout(() => {
      ApiLogout().then((res) => {
        console.log(res)
        if (res.code === 200) {
          ElMessage.success('退出成功！')
        }
      })
    })
    router.push('/login')
  }
}
</script>

<style lang="less" scoped>
.el-dropdown-link {
  color: #fff;
}

.example-showcase .el-dropdown-link {
  cursor: pointer;
  color: var(--el-color-primary);
  display: flex;
  align-items: center;
}

.container {
  height: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;

  .left {
    display: flex;
    align-items: center;
    h3 {
      color: #fff;
      padding-right: 20px;
      font-size: 24px;
      font-weight: 400;
    }
  }
}
</style>
